package littlepicture.control.commands
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import flash.events.MouseEvent;
	
	import littlepicture.control.delegates.ProjectDelegate;
	import littlepicture.model.ModelLocator;
	import littlepicture.model.RecordModel;
	import littlepicture.pojo.Record;
	import littlepicture.utils.RecordUtil;
	import littlepicture.views.Cell;
	import littlepicture.views.PlusRecord;
	import littlepicture.views.RecordDialog;
	import littlepicture.views.RecordField;
	
	import mx.containers.VBox;
	import mx.controls.Button;
	import mx.managers.PopUpManager;
	import mx.rpc.Responder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	public class PopupRecordDialogCommand implements ICommand
	{
		private var _model:ModelLocator = ModelLocator.getInstance();
		
		private var recordDialog:RecordDialog = new RecordDialog();
		
		private var data:Object;
		
		private var container:Cell;
		
		private var editMode:Boolean;
		 
		public function PopupRecordDialogCommand(){
		}
		
		public function execute(event:CairngormEvent):void{
			data = event.data;
			popupRecordDialog();
			if(data is Cell){
				container = data as Cell;
	    		createEntry(container);
	  		}else if(data is PlusRecord){
	  			container = (data as PlusRecord).parent as Cell;
	  			createEntry(container);
	  		}else{
	  			container = (data as RecordField).parent as Cell;
	  			recordDialog.title = "  Edit Entry";
	  			var recordModel:RecordModel = (data as RecordField).recordModel;
				recordDialog.recordModel = recordModel;	  
				editMode = true;				
	  		}
	    	recordDialog.init();
	    	hookEventHandler();
		}
		
		private function createEntry(container:Cell):void{
			recordDialog.title = "  Create New Entry";
	    	var recordModel:RecordModel = new RecordModel();
	    	recordModel.employee = container.employee;
	    	recordModel.status = "ACTIVE";
			recordDialog.recordModel = recordModel;  
			editMode = false;
		}
		
	    private function popupRecordDialog():void{
			PopUpManager.addPopUp(recordDialog, _model.systemManager.document.bodyPanel as VBox, false);
		    PopUpManager.centerPopUp(recordDialog);	    	
	    }
	    
	    private function hookEventHandler():void{
	    	recordDialog.saveButton.addEventListener(MouseEvent.CLICK, updateWorkRecord);
	    	recordDialog.deleteButton.addEventListener(MouseEvent.CLICK, updateWorkRecord);
	    	recordDialog.cancelButton.addEventListener(MouseEvent.CLICK, updateWorkRecord);
	    }
	    
	    private function updateWorkRecord(event:MouseEvent):void{
	    	var target:Button = event.currentTarget as Button;
	    	var projectDelegate:ProjectDelegate;
	    	
	    	if(target.label == "Save"){
	    		projectDelegate = ProjectDelegate.getInstance(new Responder(saveWorkRecord,faultResult));
	    		var record:Record = new Record();
	    		var recordModel:RecordModel = recordDialog.getRecordModel();
	    		record.status = recordModel.status;
	    		record.content = recordModel.content;
	    		record.recordId = recordModel.recordId;
	    		if(record.recordId == 0){
	    			//Create a new work record
		    		projectDelegate.addWorkRecord(recordModel.employee.employeeId, container.date, record);
	    		}else{
		    		//Update a work record
		    		projectDelegate.updateWorkRecord(record);
	    		}
	    	}else if(target.label == "Delete"){
	    		projectDelegate = ProjectDelegate.getInstance(new Responder(deleteWorkRecord,faultResult));
	    		projectDelegate.deleteWorkRecord(recordDialog.getRecordModel().recordId);
	    	}else if(target.label == "Cancel"){
	    		cancelWorkRecord();
	    	}
	    }
	    
	    private function saveWorkRecord(event:ResultEvent):void{
	    	if(!editMode){
	    		//Create Mode
	    		var childSize:int = container.getChildren().length;
	    		var recordModel:RecordModel = recordDialog.getRecordModel();
	    		recordModel.recordId = event.result as Number;
	    		if( childSize == 0){
					container.addChild(RecordUtil.createRecordText(recordModel));
	    		}else{
	    			container.addChildAt(RecordUtil.createRecordText(recordModel),childSize - 1);			                     
	    		}
	    	}else{
	    		//Edit Mode
	    		var index:int = container.getChildIndex(data as RecordField);
	    		container.removeChildAt(index);
	    		container.addChildAt(RecordUtil.createRecordText(recordDialog.getRecordModel()), index);
	    	}
			closeRecordDialog();
			container.checkCellStatus();
		}
		
		private function deleteWorkRecord(event:ResultEvent):void{
			if(editMode){
				if(container.getChildren().length == 2){
				  container.removeAllChildren();
				}else{
				  var index:int = container.getChildIndex(data as RecordField);
	    		  container.removeChildAt(index);
				}
			}
			closeRecordDialog();
			container.checkCellStatus();
		}
		
		private function cancelWorkRecord():void{
			closeRecordDialog();
		} 
		
		private function closeRecordDialog():void{
			PopUpManager.removePopUp(recordDialog);
		}
		
		private function faultResult(event:FaultEvent):void{
   			trace(event.fault);	
   		} 
	}
}